<?php	
	require_once KLEO_LIB_DIR.'core.lib.php';

	class blog extends blogmodel{
		/* Call to the model Class */
		function __construct(){
			$this->apploadlib(KLEO_LIB_DIR.'lang.lib.php', 'kleolang', $kleolang);
			require_once KLEO_MODULES_DIR.'comments/lang/'.KLEO_LANG.'/main.php';
			$this->index();
		}
		function load_the_blog($limit, &$buff){
			$this->apploadlib(KLEO_MODULES_DIR."comments/controllers/comments.php", "comments", $comments);
			$this->posts = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."blog LIMIT 2");
			$url = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];

			if(!isset($_GET['p'])){
				$cbuff = NULL;
				$cfbuff = NULL;
				$this->postid = null;
				if(isset($_GET['cat'])){
					$tid = $_GET['cat'];
					if(!is_numeric($tid)){
						$this->apperror('category id must be numeric!', "common");
					}
					$query = "SELECT * FROM ".KLEO_TABLE_PREFIX."blog, ".KLEO_TABLE_PREFIX."blog_types, ".KLEO_TABLE_PREFIX."topics, ".KLEO_TABLE_PREFIX."assoc WHERE ".KLEO_TABLE_PREFIX."blog.status = 1 AND ".KLEO_TABLE_PREFIX."topics.tstatus = 1 AND ".KLEO_TABLE_PREFIX."assoc.associtem = ".KLEO_TABLE_PREFIX."blog.id AND ".KLEO_TABLE_PREFIX."assoc.assoctopic = ".KLEO_TABLE_PREFIX."topics.topicid AND ".KLEO_TABLE_PREFIX."assoc.assoctype = 1 AND ".KLEO_TABLE_PREFIX."blog.password = '' AND ".KLEO_TABLE_PREFIX."assoc.assoctopic = $tid AND ".KLEO_TABLE_PREFIX."assoc.assoctype = 1 AND ".KLEO_TABLE_PREFIX."blog.type = ".KLEO_TABLE_PREFIX."blog_types.tyid AND ".KLEO_TABLE_PREFIX."blog.home = 1 AND NOW() <= ".KLEO_TABLE_PREFIX."blog.expires AND NOW() >= ".KLEO_TABLE_PREFIX."blog.display GROUP BY  ".KLEO_TABLE_PREFIX."assoc.associtem ORDER BY ".KLEO_TABLE_PREFIX."blog.id DESC";
				} else {
					$query = "SELECT * FROM ".KLEO_TABLE_PREFIX."blog, ".KLEO_TABLE_PREFIX."blog_types WHERE ".KLEO_TABLE_PREFIX."blog.status = 1 AND ".KLEO_TABLE_PREFIX."blog.password = '' AND ".KLEO_TABLE_PREFIX."blog.type = ".KLEO_TABLE_PREFIX."blog_types.tyid AND ".KLEO_TABLE_PREFIX."blog.home = 1 AND NOW() <= ".KLEO_TABLE_PREFIX."blog.expires AND ".KLEO_TABLE_PREFIX."blog.type != 2 AND NOW() >= ".KLEO_TABLE_PREFIX."blog.display ORDER BY ".KLEO_TABLE_PREFIX."blog.id DESC";
				}

			} else {
				$comments->generate_comments_box(1,$cfbuff);
				$this->postid = $_GET['p'];
				if(!is_numeric($this->postid)){
					$this->apperror(L_BAD_GIRL, "common");
				}
				if(!is_numeric($this->postid)){
						$this->apperror('The post id must be numeric!', "common");
				}
				$comments->get_comments($this->postid, 1, $cbuff);
				if(empty($cbuff)){
					$cbuff = '<div><small>'.L_NO_COMMENTS_YET.'</small></div>';
				}
				if(!is_numeric($this->postid)){
					$this->apperror('$this->postid must be numeric', "common");
				}

				$query = "SELECT * FROM ".KLEO_TABLE_PREFIX."blog, ".KLEO_TABLE_PREFIX."blog_types WHERE ".KLEO_TABLE_PREFIX."blog.status = 1 AND ".KLEO_TABLE_PREFIX."blog.id = $this->postid AND  ".KLEO_TABLE_PREFIX."blog.password = '' AND ".KLEO_TABLE_PREFIX."blog.type = ".KLEO_TABLE_PREFIX."blog_types.tyid AND NOW() <= ".KLEO_TABLE_PREFIX."blog.expires AND NOW() >= ".KLEO_TABLE_PREFIX."blog.display ORDER BY ".KLEO_TABLE_PREFIX."blog.id DESC";

				
				// We're showing a specific post...
				$this->showing = true;

				// If do_comment is a post value...
				if(isset($_POST['do_comment'])){

					/* Sections
					 * 1=blog,2=gallery,3=downloads,4=files
					 * We're in the blog section... so.. we choose the number 1 :-P
					 */
					$name = $_POST['name'];
					$email = $_POST['email'];
					$web = $_POST['web'];
					$comment = $_POST['text'];

					// We add the comment (you can found this function in /modules/comments/controllers/comments.php file)
					// Post ID, section, name, email, web, comment, Aid
					$comments->add_comment($this->postid, 1, $name, $email, $web, $comment, $_SESSION['uid']);

					$this->appreload();
		
				}

			}
	
			$this->set_pagination_values($limit, $query, $url, $q);

			/* Start the 'buff' variable... */
			$blog_output = "";
			while($row = $this->fetch_array($q)){
				if(isset($_GET['cat'])){
					unset($this->data['breadcrumb']);
					$this->data['breadcrumb'] = '<a href="index.php" title="'.L_HOME.'">'.L_HOME.'</a> » <a href="?module=blog" title="Blog">Blog</a> » <a href="#">'.L_TOPICS.'</a> » <a href="?module=blog&cat='.$_GET['cat'].'">'.$row['tname'].'</a>';
					$this->set_title(KLEO_WEB_TITLE.' | '.$row['tname'], $buff, $buff);
				}
				// The template lib requires this SESSION var...
				$_SESSION['postid'] = $row['id'];
				if(isset($_GET['p'])){
					unset($this->data['breadcrumb']);
					$this->data['breadcrumb'] = '<a href="index.php" title="'.L_HOME.'">'.L_HOME.'</a> » <a href="?module=blog" title="Blog">Blog</a>  » <a href="?module=blog&p='.$row['id'].'" title="'.$row['title'].'">'.$row['title'].'</a>';
					$this->set_title(KLEO_WEB_TITLE.' | '.$row['title'], $buff, $buff);
				}
				if(isset($this->showing)){
					/* If the user submit the form... */
					if(isset($_POST['id'.$row['id']])){
						if($_POST['pass_post'.$row['id']] == $row['password']){
							$_SESSION['pass_post'.$row['id']] = true;
						}
					}
					if(!empty($row['password']) && !isset($_SESSION['pass_post'.$row['id']])){
						$_SESSION['insert_pass'] = $row['id'];
						$this->apperror(L_INSERT_PASSWORD, "pass");
					}
				}


				$numcomments = $comments->get_number_comments($row['id'], 1);
					$parent = $row['id'];
					$childs_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."blog WHERE parent = $parent");
					if($this->num_rows($childs_query) !== 0){
						$childs = '<ul>';
						while($childs_row = $this->fetch_array($childs_query)){
							$childs .= '<a href="?module=blog&p='.$childs_row['id'].'"><li>'.$childs_row['title'].'</li></a>';
						}
						$childs .= '</ul>';
					} else {
						$childs = null;
					}
				$tags = $this->get_the_tags($row['id']);
				$topics = $this->get_the_topics($row['id'],1);
				$this->tpl_load($row['tyfile'], $bbuff, false, 'blog');
				$this->tpl_set(array(
						"POST_TITLE" => utf8_encode($row['title']), 
						"POST_DATE" => $row['posted'], 
						"POST_ID" => $row['id'],
						"POST_AUTHOR" => utf8_encode($this->get_username($row['aid'])),
						"POST_AUTHOR_ID" => $row['aid'],
						"POST_CONTENT" => utf8_encode($row['content']),
						"POST_FOOTER" => $row['footer'],
						"NUM_COMMENTS" => $numcomments,
						"COMMENTS_BOX" => utf8_encode($cbuff),
						"COMMENTS_FORM" => utf8_encode($cfbuff),
						"POST_TAGS" => $tags,
						"POST_TOPICS" => $topics,
						"POST_CHILDS" => $childs,
						),
					false,$bbuff,$bbuff);
				$blog_output .= $bbuff;				
			}

			$this->generate_pagination_box($pagination, $query);
			// CHANANANANNN!
			$this->tpl_set("BREADCRUMB", $this->data['breadcrumb'], $bbuff, $buff);
			$this->tpl_set(array(
			  "WEB_CONTENT" => $blog_output,
			  "PAGINATION_BOX" => $pagination
			  ), false, $buff, $buff);
			$this->compile($bbuff, $buff);
		}

		function set_expired($id){
			return $this->query("UPDATE `".KLEO_TABLE_PREFIX."blog` SET `status` = 2 WHERE `id` = $id LIMIT 1 ;");
		}
		function is_single(){
			if(isset($_GET['p']) or isset($this->showing)){
				return true;
			} else {
				return false;
			}
		}
	}
?>
